<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: modules_shipping.php 69 2013-09-21 12:00:10Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 69 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-21 12:00:10 +0000 (Sat, 21 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

require('includes/application_top.php');
require(DIR_FS_CATALOG.DIR_WS_CLASSES.'class.price.php');
$price = new price($_SESSION['currency'], '', '');

$module_type = 'shipping';
$module_directory = DIR_FS_CATALOG_MODULES.'shipping/';
$module_key = 'MODULE_SHIPPING_INSTALLED';
define('HEADING_TITLE', HEADING_TITLE_MODULES_SHIPPING);

switch ($_GET['action']) {
	case 'save':
		while (list($key, $value) = each($_POST['configuration']))
			$db->db_query("UPDATE ".TABLE_CONFIGURATION." SET configuration_value = '".$value."' WHERE configuration_key = '".$key."'");
		
		redirect(href_link('modules_shipping.php', 'module='.$_GET['module']));
	break;

	case 'install':
	case 'remove':
		$file_extension = substr($_SERVER['PHP_SELF'], strrpos($_SERVER['PHP_SELF'], '.'));
		$class = basename($_GET['module']);
		if (file_exists($module_directory.$class.$file_extension)) {
			include($module_directory.$class.$file_extension);
			$module = new $class(0);
			if ($_GET['action'] == 'install')
				$module->install();
			elseif ($_GET['action'] == 'remove')
				$module->remove();
		}
		redirect(href_link('modules_shipping.php', 'module='.$class));
	break;
}
require(DIR_WS_INCLUDES.'metatag.php');
?>
</head>
<body>
<?php require(DIR_WS_INCLUDES.'header.php'); ?>
<div id="wrapper">
<table class="outerTable" cellpadding="0" cellspacing="0">
  <tr>
    <td class="columnLeft2" width="<?php echo BOX_WIDTH; ?>" valign="top">
		<?php require(DIR_WS_INCLUDES.'column_left.php'); ?>
	</td>
    <td class="boxCenter" valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="2">
      <tr>
        <td width="100%"><table class="table_pageHeading" border="0" width="100%" cellspacing="0" cellpadding="0">
		  <tr>
		    <td class="pageHeading"><?php echo HEADING_TITLE; ?></td>
		  </tr>
		</table> </td>
      </tr>
      <tr>
        <td><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td valign="top"><table width="100%" cellspacing="0" cellpadding="0" class="dataTable">
              <tr class="dataTableHeadingRow">
                <td class="dataTableHeadingContent"><?php echo TABLE_HEADING_MODULES; ?></td>
				<td class="dataTableHeadingContent"><?php echo TABLE_HEADING_FILENAME; ?></td>
                <td class="dataTableHeadingContent" align="right"><?php echo TABLE_HEADING_SORT_ORDER; ?></td>
                <td class="dataTableHeadingContent" align="center">Status</td>
                <td class="dataTableHeadingContent" align="right"><?php echo TABLE_HEADING_ACTION; ?>&nbsp;</td>
              </tr>
				<?php
				  $file_extension = substr($_SERVER['PHP_SELF'], strrpos($_SERVER['PHP_SELF'], '.'));
				  $directory_array = array();
				  if ($dir = @dir($module_directory)) {
				    while ($file = $dir->read()) {
				      if (!is_dir($module_directory.$file)) {
				        if (substr($file, strrpos($file, '.')) == $file_extension) {
				          $directory_array[] = $file;
				        }
				      }
				    }
				    sort($directory_array);
				    $dir->close();
				  }

				  $installed_modules = array();
				  for ($i = 0, $n = sizeof($directory_array); $i < $n; $i++) {
				    $file = $directory_array[$i];

				    include(DIR_FS_LANGUAGES.$_SESSION['language'].'/modules/shipping/'.$file);
				    include($module_directory.$file);

				    $class = substr($file, 0, strrpos($file, '.'));
				    if (class_exists($class)) {
				      $module = new $class();
				      if ($module->check() > 0) {
				        if ($module->sort_order > 0) {
							if(!$installed_modules[$module->sort_order])
							    $installed_modules[$module->sort_order] = $file;
							else
							    $installed_modules[] = $file;
				        } else
				          $installed_modules[] = $file;
				      }

				      if (((!$_GET['module']) || ($_GET['module'] == $class)) && (!$mInfo)) {
				        $module_info = array('code' => $module->code,
				                             'title' => $module->title,
				                             'description' => $module->description,
				                             'status' => $module->check());
				        $module_keys = $module->keys();

				        $keys_extra = array();
				        for ($j = 0, $k = sizeof($module_keys); $j < $k; $j++) {
				          $key_value = $db->db_query("SELECT configuration_key,configuration_value, use_function, set_function FROM ".TABLE_CONFIGURATION." WHERE configuration_key = '".$module_keys[$j]."' ");
				          if ($key_value->fields['configuration_key'] !='')
				          	$keys_extra[$module_keys[$j]]['title'] = constant(strtoupper($key_value->fields['configuration_key'] .'_TITLE'));
				          $keys_extra[$module_keys[$j]]['value'] = $key_value->fields['configuration_value'];
				          if ($key_value->fields['configuration_key'] !='')
				          	$keys_extra[$module_keys[$j]]['description'] = constant(strtoupper($key_value->fields['configuration_key'] .'_DESC'));
				          $keys_extra[$module_keys[$j]]['use_function'] = $key_value->fields['use_function'];
				          $keys_extra[$module_keys[$j]]['set_function'] = $key_value->fields['set_function'];
				        }

				        $module_info['keys'] = $keys_extra;

				        $mInfo = new objectInfo($module_info);
				      }

				      if ( (is_object($mInfo)) && ($class == $mInfo->code) ) {
				        if ($module->check() > 0) {
				          echo '<tr class="dataTableRowSelected" onmouseover="this.style.cursor=\'pointer\'" onclick="document.location.href=\''.href_link('modules_shipping.php', 'module='.$class.'&action=edit').'\'">'."\n";
				        } else {
				          echo '<tr class="dataTableRowSelected">'."\n";
				        }
				      } else {
				        echo '<tr class="'.(($i%2==0)?'dataTableRow':'').'" onmouseover="this.className=\'dataTableRowOver\';this.style.cursor=\'pointer\'" onmouseout="this.className=\''.(($i%2==0)?'dataTableRow':'').'\'" onclick="document.location.href=\''.href_link('modules_shipping.php', 'module='.$class).'\'">'."\n";
				      }
				?>
                <td class="dataTableContent">
	                <?php
	                	echo $module->title;
	                	if ($module->icons_available !='')
	                		echo '<br />'.$module->icons_available; 
	                ?>
                </td>
				<td class="dataTableContent"><?php echo str_replace('.php','',$file); ?></td>
                <td class="dataTableContent" align="center"><?php if (is_numeric($module->sort_order)) echo $module->sort_order; ?>&nbsp;</td>
                <td class="dataTableContent" align="center"><?php echo ($module->_check > 0 ? image(DIR_WS_IMAGES.'icons/'.'status_'.($module->enabled ? '1' : '0' ).'.png', '') : ''); ?></td>
                <td class="dataTableContent" align="right">
                	<?php
                	if ( (is_object($mInfo)) && ($class == $mInfo->code) ) {
                		echo image(DIR_WS_IMAGES.'icon_arrow_right.gif');
                	} else {
                		echo '<a href="'.href_link('modules_shipping.php', 'module='.$class).'">'.image(DIR_WS_IMAGES.'icon_info.gif', IMAGE_ICON_INFO).'</a>';
                	} ?>
                	&nbsp;
                </td>
              </tr>
			<?php
			    }
			  }

			  ksort($installed_modules);
			  $check = $db->db_query("SELECT configuration_value from ".TABLE_CONFIGURATION." WHERE configuration_key = '".$module_key."'");
			  if ($check->_numOfRows) {
			    if ($check->fields['configuration_value'] != implode(';', $installed_modules)) {
			      $db->db_query("UPDATE ".TABLE_CONFIGURATION." set configuration_value = '".implode(';', $installed_modules)."', last_modified = now() WHERE configuration_key = '".$module_key."'");
			    }
			  } else {
			    $db->db_query("INSERT INTO ".TABLE_CONFIGURATION." (configuration_key, configuration_value, configuration_group_id, sort_order, date_added) VALUES ( '".$module_key."', '".implode(';', $installed_modules)."','6', '0', now())");
			  }
			?>
              <tr>
                <td colspan="5" class="smallText"><i><?php echo TEXT_MODULE_DIRECTORY.' '.$module_directory; ?></i></td>
              </tr>
            </table></td>
			<?php
			  $heading = array();
			  $contents = array();
			  switch ($_GET['action']) {
					case 'removepaypal':
						$heading[] = array('text' => '<b>'.$mInfo->title.'</b>');
						$contents = array ('form' => draw_form('modules', 'modules_shipping.php', 'module='.$_GET['module'].'&action=remove'));
						$contents[] = array ('text' => '<br />'.TEXT_INFO_DELETE_PAYPAL.'<br /><br />'.$mInfo->description);
						$contents[] = array ('text' => '<br />'.draw_checkbox_field('paypaldelete').' '.BUTTON_MODULE_REMOVE);
						$contents[] = array ('align' => 'center', 'text' => '<br /><button type="submit" class="button" onclick="this.blur();">'.BUTTON_START.'</button> <a class="button" onclick="this.blur();" href="'.href_link('modules_shipping.php', 'module='.$_GET['module']).'">'.BUTTON_CANCEL.'</a>');
						break;
			    case 'edit':
			      $keys = '';
			      reset($mInfo->keys);
			      while (list($key, $value) = each($mInfo->keys)) {
			        $keys .= '<b>'.$value['title'].'</b><br />'. $value['description'].'<br />';
			        if ($value['set_function']) {
					if($value['set_function'] == 'cfg_textarea(')
          					$keys .= draw_textarea_field('configuration['.$key.']','soft','30','5',$value['value']);
          				else
			          		eval('$keys .= '.$value['set_function']."'".$value['value']."', '".$key."');");
			        } else {
			          $keys .= draw_input_field('configuration['.$key.']', $value['value']);
			        }
			        $keys .= '<br /><br />';
			      }
			      $keys = substr($keys, 0, strrpos($keys, '<br /><br />'));

			      $heading[] = array('text' => '<b>'.$mInfo->title.'</b>');

			      $contents = array('form' => draw_form('modules', 'modules_shipping.php', 'module='.$_GET['module'].'&action=save'));
			      $contents[] = array('text' => $keys);
			      $contents[] = array('align' => 'center', 'text' => '<br /><button type="submit" class="button" onclick="this.blur();">'.BUTTON_UPDATE.'</button> <a class="button" onclick="this.blur();" href="'.href_link('modules_shipping.php', 'module='.$_GET['module']).'">'.BUTTON_CANCEL.'</a>');
			      break;

			    default:
			      $heading[] = array('text' => '<b>'.$mInfo->title.'</b>');

			      if ($mInfo->status > 0) {
			        $keys = '';
			        reset($mInfo->keys);
			        while (list(, $value) = each($mInfo->keys)) {
			          $keys .= '<b>'.$value['title'].'</b><br />';
			          if ($value['use_function']) {
			            $use_function = $value['use_function'];
			            if (preg_match('/->/', $use_function)) {
			              $class_method = explode('->', $use_function);
			              if (!is_object(${$class_method[0]})) {
			                include(DIR_WS_CLASSES.$class_method[0].'.php');
			                ${$class_method[0]} = new $class_method[0]();
			              }
			              $keys .= call_function($class_method[1], $value['value'], ${$class_method[0]});
			            } else {
			              $keys .= call_function($use_function, $value['value']);
			            }
			          } else {
					  if(strlen($value['value']) > 30) {
					  $keys .=  substr($value['value'],0,30).' ...';
					  } else {
			            $keys .=  $value['value'];
						}
			          }
			          $keys .= '<br /><br />';
			        }
			        $keys = substr($keys, 0, strrpos($keys, '<br /><br />'));

			        $contents[] = array('align' => 'center', 'text' => '<br /><a class="button" onclick="this.blur();" href="'.href_link('modules_shipping.php', 'module='.$mInfo->code.'&action=remove').'">'.BUTTON_MODULE_REMOVE.'</a> <a class="button" onclick="this.blur();" href="'.href_link('modules_shipping.php', 'module='.$_GET['module'].'&action=edit').'">'.BUTTON_EDIT.'</a>');
			        $contents[] = array('text' => '<br />'.$mInfo->description);
			        $contents[] = array('text' => '<br />'.$keys);
			      } else {
			        $contents[] = array('align' => 'center', 'text' => '<br /><a class="button" onclick="this.blur();" href="'.href_link('modules_shipping.php', 'module='.$mInfo->code.'&action=install').'">'.BUTTON_MODULE_INSTALL.'</a>');
			        $contents[] = array('text' => '<br />'.$mInfo->description);
			      }
			      break;
			  }

			  if ( (not_null($heading)) && (not_null($contents)) ) {
			    echo '<td width="25%" class="border" valign="top">'."\n";

			    $box = new box;
			    echo $box->infoBox($heading, $contents);

			    echo '</td>'."\n";
			  }
			?>
          </tr>
        </table></td>
      </tr>
    </table></td>
  </tr>
</table>
</div>
<?php require(DIR_WS_INCLUDES.'footer.php'); ?>
</body>
</html>
<?php require(DIR_WS_INCLUDES.'application_bottom.php'); ?>